/**
 *	插件:reim的使用
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */


package com.framework.Chajian;


import com.framerock.baselib.REIM;
import com.framerock.cache.Cache;
import com.framerock.db.DBConn;
import com.framerock.server.WebSocketClient;
import com.framerock.baselib.CLog;
import com.framerock.baselib.Config;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockMsg;
import com.framerock.baselib.Xinhu;
import com.framework.ACore.Chajian;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChajianReim extends Chajian {

    private String service_recid = "",service_push = "",service_cehui="";
    private Boolean inbo = false;

    public ChajianReim(DBConn dbs) {
        super(dbs);
    }

    public static ChajianReim obj(DBConn dbs)
    {
        return new ChajianReim(dbs);
    }

    /***
     * 获取信息
     */
    public String getInfo()
    {
        String service_url   = Xinhu.getVal(this.nDb, "service_url", "auto");
        service_recid        = Xinhu.getVal(this.nDb, "service_recid", "xinhujava");
        service_cehui        = Xinhu.getVal(this.nDb, "service_cehui", "2");

        Json json = new Json();
        json.putVal("url", service_url);
        json.putVal("recid", service_recid);
        json.putVal("cehui", service_cehui);
        return json.getJson();
    }

    //读取配置
    private void getConfig()
    {
        service_recid = Xinhu.getVal(this.nDb,"service_recid", "xinhujava");
        service_push  = Xinhu.getVal(this.nDb, "service_push");
        service_cehui = Xinhu.getVal(this.nDb,"service_cehui", "2");
        inbo = true;
    }

    /**
     * 推送reim消息
     * */
    public RockMsg push(Map<String, String> da)
    {
        RockMsg rockmsg = RockMsg.obj();
        if(!inbo)getConfig();
        if(Rock.isEmpt(service_recid))
            return rockmsg.error("not config reimservice");

        Map<String, String> arr = Rock.getMap();
        arr.put("from", service_recid);
        arr.put("qtype", "reim");//固定reim
        arr.put("atype", "send"); //类型send,getonline
        arr.put("receid", "");//[必须的]

        String str = "";
        str = Rock.getMapString(da, "title");
        if(!Rock.isEmpt(str))da.put("title", Jiami.base64encode(str));

        str = Rock.getMapString(da, "mess");
        if(!Rock.isEmpt(str))da.put("mess", Jiami.base64encode(str));

        for (Map.Entry<String, String> entry : da.entrySet())
            arr.put(entry.getKey(), entry.getValue());


        //没开启redis
        if(!Cache.isOpen()) {
            WebSocketClient.send(arr);
        }else{
            String receid = Rock.getMapString(arr, "receid");
            String recead[],key,nurl,recid="";
            recead  = receid.split(",");
            Map<String, List<String>> recidList = new HashMap<String, List<String>>();
            List<String> list;
            for(int i=0;i<recead.length;i++){
                key = ""+service_recid+"_"+recead[i]+"";
                nurl= Cache.get(key);
                //CLog.debug("key:"+key+"="+nurl);
                if(!Rock.isEmpt(nurl)){
                    list = recidList.get(nurl);
                    if(list == null)recidList.put(nurl, new ArrayList<String>());
                    recidList.get(nurl).add(recead[i]);
                }
            }
            for(Map.Entry<String, List<String>> entry:recidList.entrySet()){
                nurl    = entry.getKey();
                recid   = "";
                list    = entry.getValue();
                for(int i=0;i<list.size();i++){
                    recid += ",";
                    recid += list.get(i);
                }
                if(!Rock.isEmpt(recid)) {
                    recid = recid.substring(1);
                    arr.put("receid", recid);
                    //CLog.debug(nurl+":"+Json.getJsonString(arr));
                    REIM.push(nurl+"reim/push", arr);
                }
            }
        }
        return rockmsg.success();
    }



    /**
     * 启动计划任务(弃用)
     * */
    public RockMsg taskstart()
    {
        if(!inbo)getConfig();
        String jar = ""+Config.ROOT_PATH+"/xinhujava.jar";
        if(!new File(jar).exists())return RockMsg.obj().error(""+jar+"文件不存在");
        String url = "java -jar "+jar+" config="+ Config.PATH_INI+" action=taskrun";
        return push(Rock.getMap(new String[]{
                "qtype","starturl",
                "type","task",
                "recid",service_recid,
                "url",url
        }));
    }
    
    /**
     * 启动计划任务(弃用)
     * */
    public RockMsg taskstop()
    {
        if(!inbo)getConfig();
        return push(Rock.getMap(new String[]{
                "qtype","starturl",
                "type","task",
                "recid",service_recid,
                "url",""
        }));
    }
}
